layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

        
        $.get("technologyType.json", function (res) {
            //模拟插入
            var List = [],
            data = res.data;
            if(data){
                for (var i = 0; i < data.length; i++) {
                    List.push('<option value="'+data[i].name+'">'+data[i].name+'</option>');
                }
                List.push('<option value="其他">其他</option>');
                $('.technologyTpyeStatus').append(List);
                form.render('select');
            }
        });

    //上传缩略图
    upload.render({
        elem: '.thumbBox',
        url: 'http://127.0.0.1:8000/uploadImg',
        method: "post",  
        
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                //console.log(result.data.src)
                console.log(result)
                
                $('.thumbBox').css("background", "#fff");
            });
        },
        done: function (res) {
            console.log(res)
            $('.thumbImg').attr('src', res.data.src);


        },
    });
   
    //图书发布提交
    form.on("submit(bookAddCommit)",function(data){
        //截取文章内容中的一部分文字放入文章摘要
        //var abstract = layedit.getText(editIndex).substring(0,50);
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //console.log($(".bookName").val())
        admin = localStorage.getItem("admin"); 
        //实际使用时的提交信息
        $.post("http://127.0.0.1:8000/bookedit",{

            userId:admin,//用户id
            pid: $(".mypid").val(), //pid
            bookConnect : $(".bookConnect").val(), 
            bookName : $(".bookName").val(),  //文章标题
            bookabstract : $(".bookabstract").val(),  //文章摘要
            // content : layedit.getContent(editIndex).split('<audio controls="controls" style="display: none;"></audio>')[0],  //文章内容
            bookImg : $(".thumbImg").attr("src"),  //缩略图
            classify : $(".technologyTpyeStatus option:selected").text(),    //文章分类
            // newsStatus : $('.newsStatus select').val(),    //发布状态
            // newsTime : submitTime,    //发布时间
            // newsTop : data.filed.newsTop == "on" ? "checked" : "",    //是否置顶
        },function(res){
        
        })
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("文章修改成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },500);
        return false;
    })
    form.on('select(technologyTpyeStatus)', function(data){
        // console.log(data);
        // console.log(data.elem); //得到select原始DOM对象
        // console.log(data.value); //得到被选中的值
        // console.log(data.othis); //得到美化后的DOM对象
        if(data.value == "其他"){
            $(".otherTechnology").removeClass("layui-hide");
            $(".otherTechnology #otherTechnology").attr("lay-verify","required");
        }else{
            $(".otherTechnology").addClass("layui-hide");
            $(".otherTechnology #otherTechnology").removeAttr("lay-verify");
        }
    });
    editIndex = layedit.build('news_content', {
        height: 30,
        tool: ['link' //加粗
        
        ],
        uploadImage : {
            url : "http://127.0.0.1:8000/uploadImg"
        }
    });
    // //用于同步编辑器内容到textarea
    layedit.sync(editIndex);
})